/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.test.gl;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.util.Log;



/**
 * This is a port of the {@link http://nehe.gamedev.net} OpenGL
 * tutorials to the Android 1.5 OpenGL ES platform. Thanks to
 * NeHe and all contributors for their great tutorials and great
 * documentation. This source should be used together with the
 * textual explanations made at {@link http://nehe.gamedev.net}.
 * The code is based on the original Visual C++ code with all
 * comments made. It has been altered and extended to meet the
 * Android requirements. The Java code has according comments.
 *
 * If you use this code or find it helpful, please visit and send
 * a shout to the author under {@link http://www.insanitydesign.com/}
 *
 * @DISCLAIMER
 * This source and the whole package comes without warranty. It may or may
 * not harm your computer or cell phone. Please use with care. Any damage
 * cannot be related back to the author. The source has been tested on a
 * virtual environment and scanned for viruses and has passed all tests.
 *
 *
 * This is an interpretation of "Lesson 06: Texture Mapping"
 * for the Google Android platform.
 *
 * @author Savas Ziplies (nea/INsanityDesign)
 * 
 * * Modify logics based on Savas's Quad class.
* @author g2southpark
*/

 public class QuadRender implements Renderer {
	 private static final String TAG ="QuadRender";
	GL11 mGL;
    private Quad quad;
    
    /* Rotation values for all axis */
    private float xrot;            //X Rotation ( NEW )
    private float yrot;            //Y Rotation ( NEW )
    private float zrot;            //Z Rotation ( NEW )

    
    /** The Activity Context ( NEW ) */
    private Context context;
    private GLSurfaceView glview;
    /**
     * Instance the Cube object and set
     * the Activity Context handed over
     */
    public QuadRender(Context context, GLSurfaceView glsurface) {
       this.context = context;
       glview = glsurface;
       quad = new Quad();
 
    }

    /**
     * The Surface is created/init()
     */
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {      


        GL11 gl2 = (GL11) gl;
        if (mGL == null) {
            mGL = gl2;
        } else {
            // The GL Object has changed.
            Log.i(TAG, "GLObject has changed from " + mGL + " to " + gl);
            mGL = gl2;
        }
        
        if (false) {
        	glview.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
        } else {
        	glview.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        }
  

        // Increase the priority of the render thread.
        // This is commented out to give other threads more CPU.
        //Process.setThreadPriority(Process.THREAD_PRIORITY_DISPLAY);

        // Disable unused state.
        gl.glEnable(GL11.GL_DITHER);
        gl.glDisable(GL11.GL_LIGHTING);

        // Set global state.
        // gl.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);

        // Enable textures.
        gl.glEnable(GL11.GL_TEXTURE_2D);
        gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_REPLACE);

        // Set up state for multitexture operations. Since multitexture is
        // currently used
        // only for layered crossfades the needed state can be factored out into
        // one-time
        // initialization. This section may need to be folded into drawMixed2D()
        // if multitexture
        // is used for other effects.

        // Enable Vertex Arrays
        gl.glEnableClientState(GL11.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
        gl.glClientActiveTexture(GL11.GL_TEXTURE1);
        gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
        gl.glClientActiveTexture(GL11.GL_TEXTURE0);

        // Enable depth test.
        gl.glEnable(GL11.GL_DEPTH_TEST);
        gl.glDepthFunc(GL11.GL_LEQUAL);

        // Set the blend function for premultiplied alpha.
        gl.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);

        // Set the background color.
        gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        gl.glClear(GL11.GL_COLOR_BUFFER_BIT);
 
       //Load the texture for the cube once during Surface creation

       quad.loadGLTexture(gl, this.context, R.drawable.default_background);
       /*    
       // Disable unused state.
       gl.glEnable(GL11.GL_DITHER);
       gl.glDisable(GL11.GL_LIGHTING);
       gl.glEnable(GL10.GL_BLEND); // 打开混合  
       // Set global state.
       // gl.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);

       // Enable textures.
       gl.glEnable(GL11.GL_TEXTURE_2D);
       gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_BLEND_DST);

       // Set up state for multitexture operations. Since multitexture is
       // currently used
       // only for layered crossfades the needed state can be factored out into
       // one-time
       // initialization. This section may need to be folded into drawMixed2D()
       // if multitexture
       // is used for other effects.

       // Enable Vertex Arrays
       gl.glEnableClientState(GL11.GL_VERTEX_ARRAY);
       gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
       gl.glClientActiveTexture(GL11.GL_TEXTURE1);
       gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
       gl.glClientActiveTexture(GL11.GL_TEXTURE0);

       // Enable depth test.
       gl.glDisable(GL11.GL_DEPTH_TEST);
       //gl.glDepthFunc(GL11.GL_LEQUAL);

       // Set the blend function for premultiplied alpha.
       gl.glEnable(GL10.GL_BLEND); // 打开混合  
       gl.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

       // Set the background color.
       gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
       gl.glClear(GL11.GL_COLOR_BUFFER_BIT);
       */
    }

    /**
     * Here we do our drawing
     */
    public void onDrawFrame(GL10 gl) {
       //Clear Screen And Depth Buffer
       gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);   
       gl.glLoadIdentity();               //Reset The Current Modelview Matrix
       
       //Drawing
       gl.glTranslatef(0.0f, 0.0f, -5.0f);      //Move 5 units into the screen
      // gl.glScalef(1.5f, 1.0f, 1.0f);          //Scale the Cube to 80 percent, otherwise it would be too large for the screen
       
       //Rotate around the axis based on the rotation matrix (rotation, x, y, z)
  //     gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);   //X
  //     gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);   //Y
  //     gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);   //Z
             
       //gl.glRotatef(270, 0.0f, 0.0f, 1.0f); //顺时针旋转90度，
       
       quad.draw(gl);                  //Draw the Quad   
       
       //Change rotation factors (nice rotation)
       xrot += 0.3f;
       yrot += 0.2f;
       zrot += 0.4f;
       gl.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
       gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    }

    /**
     * If the surface changes, reset the view
     */
    public void onSurfaceChanged(GL10 gl, int width, int height) {
       if(height == 0) {                   //Prevent A Divide By Zero By
          height = 1;                   //Making Height Equal One
       }
       // Set the viewport and projection matrix.
       final float zNear = 0.1f;
       final float zFar = 100.0f;
       gl.glViewport(0, 0, width, height);
       gl.glMatrixMode(GL11.GL_PROJECTION);
       gl.glLoadIdentity();
       GLU.gluPerspective(gl, 45.0f, (float) width / height, zNear, zFar);

       gl.glMatrixMode(GL11.GL_MODELVIEW);
       gl.glLoadIdentity();
/*
       gl.glViewport(0, 0, width, height);    //Reset The Current Viewport
       gl.glMatrixMode(GL10.GL_PROJECTION);    //Select The Projection Matrix
       gl.glLoadIdentity();                //Reset The Projection Matrix

       //Calculate The Aspect Ratio Of The Window
       GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);

       gl.glMatrixMode(GL10.GL_MODELVIEW);    //Select The Modelview Matrix
       gl.glLoadIdentity();                //Reset The Modelview Matrix
       
       */
    }
    
 
 }






